package org.library.menu;

import org.library.controller.TokenController;
import org.library.domain.SysUser;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.stereotype.Component;

/**
 * @author Chen
 * @since 2023/6/13 15:06
 */
@Component
public class RegisterMenu extends Menu {

    public RegisterMenu(BeanFactory beanFactory) {
        super(beanFactory);
    }

    @Override
    public void menu() {
        System.out.println("欢迎注册图书管理系统");
        SysUser sysUser = new SysUser();
        System.out.println("请输入昵称：");
        sysUser.setNickname(scanner.next());
        System.out.println("请输入手机号：");
        sysUser.setPhone(scanner.next());
        System.out.println("请输入密码：");
        sysUser.setPassword(scanner.next());
        System.out.println("请再次输入密码：");
        if (!sysUser.getPassword().equals(scanner.next())) {
            System.out.println("两次密码不一致");
            library.setMenu(beanFactory.getBean(MainMenu.class));
            library.run();
        } else {
            Boolean register = controllerFactory.getController(TokenController.class).register(sysUser);
            if (register) {
                System.out.println("注册成功");
                library.setMenu(beanFactory.getBean(MainMenu.class));
                library.run();
            } else {
                System.out.println("注册失败，未知错误");
                library.setMenu(beanFactory.getBean(MainMenu.class));
                library.run();
            }
        }
    }
}
